#![recursion_limit = "512"]
#![feature(todo_macro)]

use log::trace;
use yew::prelude::*;
use yew_store::{create_store_with_callback, StoreLink};

mod components;
mod fetcher;
mod index;
mod service;
mod state;
mod views;

use index::Model as Index;
use state::{Action, Event, Mode, State};

pub struct Model {
    store: StoreLink<State, Action>,
}

pub enum Msg {
    StoreEvent(Event),
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();

    fn create(_: Self::Properties, mut link: ComponentLink<Self>) -> Self {
        // trace!("Lib created.");
        let callback = link.send_back(|event| Msg::StoreEvent(event));
        let store = create_store_with_callback(State::new(), Some(callback));
        Model { store }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::StoreEvent(event) => return self.handle(event),
        }
    }
}
impl Model {
    fn handle(&mut self, event: Event) -> ShouldRender {
        // trace!("{:?}", event);
        match event {
            Event::ModeChanged => true,
            _ => false,
        }
    }
}

impl Renderable<Model> for Model {
    fn view(&self) -> Html<Self> {
        match self.store.state().mode {
            Mode::Index => html! {
                <Index store=self.store.clone() />
            }
        }
    }
}
